package com.haier.common;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


public class ResultUtils {

    public static Map<String, Object> getQueryParams(String  requesturl) {  
        Map<String, Object> map = new HashMap<String, Object>();
		String[] urlarr = requesturl.split("&");
		for(int ii=0;ii<urlarr.length;ii++){
			String[] value = urlarr[ii].split("=");
			if(value.length==2){
				map.put(value[0],value[1]);
			}else{
				map.put(value[0],"");
			}
			
			
		}
		return map;
    }
    
    public static Object toJavaBean(Object javabean, Map<String, Object> m,Boolean zhuanma)   
    {   
     //所有声明了的方法  
        Method[] methods = javabean.getClass().getDeclaredMethods();   
        for (Method method : methods)   
        {   
                if (method.getName().startsWith("set"))   
                {   
                 //得到该方法的参数类型  
                 Class<?>[] params = method.getParameterTypes();  
                    String field = method.getName();   
                    field = field.substring(field.indexOf("set") + 3);   
                    field = field.toLowerCase().charAt(0) + field.substring(1);   
                    //System.out.println(field+"----"+m.get(field.toString()));  
                    Object value=m.get(field.toString());  
                    if(value==null){
                    	continue;
                    }
                    try {  
                        // convert types for some popular ones  
                        if (value != null&&!"".equals(value)) {  
                         String pa=params[0].getName().toString();  
                                if (pa.equals("java.util.Date")) {  
                                	
                                	value = java.net.URLDecoder.decode(value.toString());
                                    //value = new java.util.Date(((java.util.Date) value).getTime());  
                                	SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/dd HH:mm:ss");
                                	 value = sdf.parse(value.toString());
                                }  
                            else if(pa.equals("java.lang.String")){  
                            	value = java.net.URLDecoder.decode(value.toString());
                              value= new java.lang.String((value.toString()));  
                             //if(zhuanma)value= new java.lang.String((value.toString()));  
                            }  
                            else if(pa.equals("java.lang.Integer")||pa.equals("int")){  
                              value= new java.lang.Integer(value.toString());  
                            }else if (pa.equals("java.lang.Long")) {  
                                 value= new java.lang.Long(value.toString());  
                            } else if (pa.equals("java.lang.Double")) {  
                              value= new java.lang.Double(value.toString());  
   
                            } else if (pa.equals("java.lang.Float")) {  
                              value= new java.lang.Float(value.toString());  
   
                            } else if (pa.equals("java.lang.Short")) {  
                              value= new java.lang.Short(value.toString());  
   
                            } else if (pa.equals("java.lang.Byte")) {  
                              value= new java.lang.Byte(value.toString());  
   
                            } else if (pa.equals("java.lang.Character")) {  
                             // value= new java.lang.Character(value);  
   
                            } else if (pa.equals("java.lang.Boolean")) {  
                              value= new java.lang.Boolean(value.toString());  
   
                            }   
                                  
                                // Don't call setter if the value object isn't the right type   
                                //if (isCompatibleType(value, params[0])) {  
                                	if ((value.getClass().getName().equals(pa))) {  
                                    method.invoke(javabean, new Object[] { value });  
                                } else {  
                                  throw new Exception(  
                                      "Cannot set " + field.toString() + ": incompatible types.");  
                                }  
                        }  
                } catch(Exception e){  
                 e.printStackTrace();  
                }  
        }   
                }  
   
        return javabean;   
    }   
    
    
    
}
